package com.yxy.f_music_server.controller.admin;

import com.yxy.f_music_server.common.PageResult;
import com.yxy.f_music_server.common.Result;
import com.yxy.f_music_server.constant.RequestMapperConstant;
import com.yxy.f_music_server.domain.dto.SongPageQueryDTO;
import com.yxy.f_music_server.domain.po.Song;
import com.yxy.f_music_server.service.MusicService;
import com.yxy.f_music_server.utils.FileCopyUtil;
import com.yxy.f_music_server.utils.QiniuOSSUtil;
import com.yxy.f_music_server.utils.SongUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController("adminMusicController")
@RequestMapping(RequestMapperConstant.ADMIN_REQUEST_MAPPER + "music")
@Slf4j
public class MusicController {

    @Autowired
    private MusicService musicService;

    /**
     * 添加音乐
     *
     * @param multipartFile
     * @return
     */
    @SneakyThrows
    @PostMapping
    public Result addMusic(@RequestParam("file") MultipartFile multipartFile) {
        log.info("添加音乐：{}", multipartFile.getOriginalFilename());
        MultipartFile copy = null;
        copy = FileCopyUtil.copy(multipartFile);

        Song song = SongUtil.getSongMetaData(copy);
        // 查询是否存在（若存在在实现类中抛出异常）
        Song s = musicService.getByTitle(song.getTitle());
        // 上传
        String src = QiniuOSSUtil.uploadToQiniu(multipartFile, song.getTitle());
        song.setSrc(src);
        musicService.add(song);
        return Result.success("添加成功");
    }
}
